//
//  UCSearchBarSetting.swift
//  UICreate
//
//  Created by 范晓鑫 on 2021/10/27.
//

import UIKit

extension UCSetting where TargetType: UISearchBar {
    ///样式
    public static func style(_ style: UIBarStyle) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.barStyle = style
        })
    }
    ///delegate
    public static func delegate(_ delegate: UISearchBarDelegate?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.delegate = delegate
        })
    }
    ///text
    public static func text(_ text: String?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.text = text
        })
    }
    ///prompt
    public static func prompt(_ prompt: String?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.prompt = prompt
        })
    }
    ///placeholder
    public static func placeholder(_ placeholder: String?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.placeholder = placeholder
        })
    }
    ///showsBookmarkButton
    public static func bookmark(_ button: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.showsBookmarkButton = button
        })
    }
    ///showsCancelButton
    public static func cancel(_ button: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.showsCancelButton = button
        })
    }
    ///showsSearchResultsButton
    public static func searchResults(_ button: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.showsSearchResultsButton = button
        })
    }
    ///isSearchResultsButtonSelected
    public static func searchResults(selected: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isSearchResultsButtonSelected = selected
        })
    }
    ///高亮色
    public static func tint(bar color:UIColor?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.barTintColor = color
        })
    }
    ///搜索条样式
    public static func style(search style: UISearchBar.Style) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.searchBarStyle = style
        })
    }
    ///半透明
    public static func translucent(_ translucent: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isTranslucent = translucent
        })
    }
    ///scopeButtonTitles
    public static func scope(buttons: [String]?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.scopeButtonTitles = buttons
        })
    }
    ///selectedScopeButtonIndex
    public static func scope(selected index: Int) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.selectedScopeButtonIndex = index
        })
    }
    ///showsScopeBar
    public static func scope(_ show: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.showsScopeBar = show
        })
    }
    ///自定义键盘 inputAccessoryView
    public static func input(accessory view: UIView?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.inputAccessoryView = view
        })
    }
    ///backgroundImage
    public static func bg(_ image: UIImage?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.backgroundImage = image
        })
    }
    ///scopeBarBackgroundImage
    public static func scope(bg image: UIImage?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.scopeBarBackgroundImage = image
        })
    }
    ///setBackgroundImage(_ backgroundImage: UIImage?, for barPosition: UIBarPosition, barMetrics: UIBarMetrics)
    public static func bg(_ image: UIImage?, for position: UIBarPosition, metrics: UIBarMetrics = .default) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setBackgroundImage(image, for: position, barMetrics: metrics)
        })
    }
    ///setSearchFieldBackgroundImage(_ backgroundImage: UIImage?, for state: UIControl.State)
    public static func text(bg image: UIImage?, for state: UIControl.State = .normal) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setSearchFieldBackgroundImage(image, for: state)
        })
    }
    ///setImage(_ iconImage: UIImage?, for icon: UISearchBar.Icon, state: UIControl.State)
    public static func icon(_ icon: UISearchBar.Icon, image: UIImage?, for state: UIControl.State = .normal) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setImage(image, for: icon, state: state)
        })
    }
    ///setScopeBarButtonBackgroundImage(_ backgroundImage: UIImage?, for state: UIControl.State)
    public static func scope(bg image: UIImage?, for state: UIControl.State = .normal) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setScopeBarButtonBackgroundImage(image, for: state)
        })
    }
    ///setScopeBarButtonDividerImage(_ dividerImage: UIImage?, forLeftSegmentState leftState: UIControl.State, rightSegmentState rightState: UIControl.State)
    public static func scope(divider image: UIImage?, left: UIControl.State = .normal, right: UIControl.State = .normal) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setScopeBarButtonDividerImage(image, forLeftSegmentState: left, rightSegmentState: right)
        })
    }
    ///setScopeBarButtonTitleTextAttributes(_ attributes: [NSAttributedString.Key : Any]?, for state: UIControl.State)
    public static func scope(attr: [NSAttributedString.Key : Any]?, for state: UIControl.State = .normal) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setScopeBarButtonTitleTextAttributes(attr, for: state)
        })
    }
    ///searchFieldBackgroundPositionAdjustment
    public static func bg(adjust: UIOffset) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.searchFieldBackgroundPositionAdjustment = adjust
        })
    }
    ///searchTextPositionAdjustment
    public static func text(adjust: UIOffset) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.searchTextPositionAdjustment = adjust
        })
    }
    ///searchTextPositionAdjustment
    public static func icon(_ icon: UISearchBar.Icon, adjust: UIOffset) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.setPositionAdjustment(adjust, for: icon)
        })
    }
}
